﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Globalization;

namespace Transport
{
    public partial class Orders : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        protected void BindData()
        {
            gvShoppingCart.DataSource = ShoppingCart.Instance.Items;
            gvShoppingCart.DataBind();
            if (null != Session["user_name"] && ShoppingCart.Instance.Items.Count > 0)
                btnUpdateCart.Visible = true;
            else
                btnUpdateCart.Visible = false;
        }

        protected void gvShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[6].Text = "Tổng cộng: " + String.Format(CultureInfo.CreateSpecificCulture("en-US"), "{0:c}", ShoppingCart.Instance.GetSubTotal());
            }
        }
        protected void gvShoppingCart_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                string carID = (string)e.CommandArgument;
                ShoppingCart.Instance.RemoveItem(carID);
            }
            BindData();
        }

        protected void btnThanhToan_Click(object sender, EventArgs e)
        {
            try
            {
                string madichvu = ShoppingCart.Instance.Items[0].CarInfo.MaDichVu;
                switch (madichvu)
                {
                    case "0":
                        {
                                chi_tiet_dat_xe value = new chi_tiet_dat_xe();

                                value.ma_lo_trinh = ShoppingCart.Instance.Items[0].CarInfo.MaLoTrinh;
                                value.gio_don = ShoppingCart.Instance.Items[0].CarInfo.GioDon;
                                value.tu_ngay = ShoppingCart.Instance.Items[0].CarInfo.TuNgay;
                                value.den_ngay = value.tu_ngay.AddDays(ShoppingCart.Instance.Items[0].CarInfo.SoNgay);
                                value.dia_diem_don = ShoppingCart.Instance.Items[0].CarInfo.DiaDiemDon;
                                value.ten_dang_nhap = Session["user_name"].ToString();
                                value.so_cho = 0;
                                value.so_luong_xe = ShoppingCart.Instance.Items.Count;
                                value.ghi_chu = ghichu.Text;
                                ChiTietDatXeControl datxe = new ChiTietDatXeControl();
                                string madatxe = datxe.insertTheoTour(value);
                                if (madatxe != "false")
                                {
                                    DanhSachXeDatControl dsCtrl = new DanhSachXeDatControl();
                                    for (int j = 0; j < ShoppingCart.Instance.Items.Count; j++)
                                    {
                                        danh_sach_xe_dat ds = new danh_sach_xe_dat();
                                        ds.ma_so_dat_xe = madatxe;
                                        ds.bien_so = ShoppingCart.Instance.Items[j].CarInfo.MaXe;
                                        bool xedat = dsCtrl.insert(ds);
                                    }

                                    int tongtien = datxe.calculateFee(madatxe);
                                    bool result = datxe.updatePhiDuKien(madatxe, tongtien);
                                    if (result)
                                    {
                                        ShoppingCart.Instance.RemoveAllItem();
                                        string redirecturl = "";
                                        redirecturl += "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" + ConfigurationManager.AppSettings["paypalemail"].ToString();
                                        redirecturl += "&item_name=" + madatxe.ToString();
                                        redirecturl += "&amount=" + tongtien;
                                        redirecturl += "&cy=USD";
                                        redirecturl += "&return=" + ConfigurationManager.AppSettings["SuccessURL"].ToString();
                                        redirecturl += "&cancel_return=" + ConfigurationManager.AppSettings["FailedURL"].ToString();
                                        Response.Redirect(redirecturl);
                                    }
                                }
                        }
                        break;
                    default:
                        {
                                chi_tiet_dat_xe value = new chi_tiet_dat_xe();

                                value.gio_don = ShoppingCart.Instance.Items[0].CarInfo.GioDon;
                                value.phuong_thuc_lai = ShoppingCart.Instance.Items[0].CarInfo.PhuongThucLai.ToString();
                                value.ma_mien = ShoppingCart.Instance.Items[0].CarInfo.Mien;
                                value.tu_ngay = ShoppingCart.Instance.Items[0].CarInfo.TuNgay;
                                value.den_ngay = ShoppingCart.Instance.Items[0].CarInfo.DenNgay;
                                value.dia_diem_don = ShoppingCart.Instance.Items[0].CarInfo.DiaDiemDon;
                                value.ma_dich_vu = ShoppingCart.Instance.Items[0].CarInfo.MaDichVu;
                                value.ten_dang_nhap = Session["user_name"].ToString();
                                value.so_cho = 0;
                                value.so_luong_xe = ShoppingCart.Instance.Items.Count;
                                value.ghi_chu = ghichu.Text;
                                ChiTietDatXeControl datxe = new ChiTietDatXeControl();
                                string madatxe = datxe.insertTheoYeuCau(value);
                                if (madatxe != "false")
                                {
                                    DanhSachXeDatControl dsCtrl = new DanhSachXeDatControl();
                                    for (int j = 0; j < ShoppingCart.Instance.Items.Count; j++)
                                    {
                                        danh_sach_xe_dat ds = new danh_sach_xe_dat();
                                        ds.ma_so_dat_xe = madatxe;
                                        ds.bien_so = ShoppingCart.Instance.Items[j].CarInfo.MaXe;
                                        bool xedat = dsCtrl.insert(ds);
                                    }

                                    int tongtien = datxe.calculateFee(madatxe);
                                    bool result = datxe.updatePhiDuKien(madatxe, tongtien);
                                    if(result)
                                    {
                                        ShoppingCart.Instance.RemoveAllItem();
                                        string redirecturl = "";
                                        redirecturl += "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" + ConfigurationManager.AppSettings["paypalemail"].ToString();
                                        redirecturl += "&item_name="+madatxe;
                                        redirecturl += "&amount=" + tongtien;
                                        redirecturl += "&cy=USD";
                                        redirecturl += "&return=" + ConfigurationManager.AppSettings["SuccessURL"].ToString();
                                        redirecturl += "&cancel_return=" + ConfigurationManager.AppSettings["FailedURL"].ToString();
                                        Response.Redirect(redirecturl);
                                    }
                                }
                        }
                        break;
                }
                
            }
            catch (FormatException) { }
        }
    }
}